﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sky.Web.Models;

namespace Sky.Web.Controllers
{
    public class StoreManagerController : Controller
    {
        StoreEntities storeDB = new StoreEntities();
        //
        // GET: /StoreManager/
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            var alums = storeDB.Albums.Include("Genre").Include("Artist");
            return View(alums.ToList());
        }

        //
        // GET: /StoreManager/Details/5

        public ActionResult Details(int id)
        {
            Album album = storeDB.Albums.Find(id);
            return View(album);
        }

        //
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(storeDB.Genres, "GenreID", "Name");
            ViewBag.ArtistId = new SelectList(storeDB.Artists, "ArtistId", "Name");
            return View();
        } 

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Album collection)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    storeDB.Albums.Add(collection);
                    storeDB.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.GenreId = new SelectList(storeDB.Genres, "GenreID", "Name");
                ViewBag.ArtistId = new SelectList(storeDB.Artists, "ArtistId", "Name");
                return View(collection);                
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /StoreManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            Album album = storeDB.Albums.Find(id);
            return View(album);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Album album)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    storeDB.Entry(album).State = System.Data.EntityState.Modified;
                    storeDB.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.GenreId = new SelectList(storeDB.Genres, "GenreId", "Name");
                ViewBag.ArtistId = new SelectList(storeDB.Artists, "ArtistId", "Name");
                return View(album);
                //// TODO: Add update logic here
                //Album album = storeDB.Albums.Find(id);
                //if (this.TryUpdateModel<Album>(album))
                //{
                //    return RedirectToAction("Index");
                //}
                //return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /StoreManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            Album album = storeDB.Albums.Find(id);
            return View(album);
        }

        //
        // POST: /StoreManager/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult Delete(int id, Album album)
        //{
        //    try
        //    {
        //        // TODO: Add delete logic here
        //        //Album album = storeDB.Albums.Find(id);
        //        storeDB.Albums.Remove(album);
        //        storeDB.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}
        //

        // POST: /StoreManager/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Album album = storeDB.Albums.Find(id);
            storeDB.Albums.Remove(album);
            storeDB.SaveChanges();
            return RedirectToAction("Index");
        }

    }
}
